package com.qf.service.impl;

import com.qf.entity.KuCunUser;
import com.qf.entity.Order;
import com.qf.entity.WmsUser;
import com.qf.mapper.ArrivalMapper;
import com.qf.service.ArrivalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author Xiaoze
 * @date 2024/7/25
 * @desc
 */
@Service
public class ArrivalServiceImpl  implements ArrivalService {
    @Autowired
    private ArrivalMapper arrivalMapper;
    @Override
    public List<Order> findAll(HashMap<String, String> map) {
        return arrivalMapper.findAll(map);
    }

    @Override
    public boolean delOrderById(int id) {
        return arrivalMapper.delOrderById(id);
    }

    @Override
    public boolean delBatch(List<Integer> ids) {
        return arrivalMapper.delBatch(ids);
    }

    @Override
    public List<Order> detail(int id) {
        return arrivalMapper.detail(id);
    }

    @Override
    public List<WmsUser> findUserName() {
        return arrivalMapper.findUserName();
    }

    @Override
    public boolean entry(Order order) {

        if(order.getArrivalStatus().equals("1")){
           KuCunUser kuCunUser = arrivalMapper.findName(order.getOrderName());
           if(kuCunUser != null ){
              return false;
           }
        } else if (order.getArrivalStatus().equals("3")) {
            KuCunUser kuCunUser = arrivalMapper.findName(order.getOrderName());
            if( kuCunUser == null ){
                order.setCargoPic("https://xiaoze-taitai.oss-cn-beijing.aliyuncs.com/"+order.getCargoPic());
                return arrivalMapper.entry(order);
            }
            if(order.getAQuantity() == kuCunUser.getAQuantity()){
                return false;
            }

            if (kuCunUser != null){
                 int aQuan =(order.getAQuantity() - kuCunUser.getAQuantity())+kuCunUser.getAQuantity();
                kuCunUser.setAQuantity(aQuan);
                kuCunUser.setInventoryNum(aQuan);
                if (kuCunUser.getAQuantity() == kuCunUser.getQuantity() ){
                    kuCunUser.setArrivalStatus("1");
                }

               return arrivalMapper.update(kuCunUser);

            }


        }

        return false;
    }
}
